package cate.game.activity.weekendgift;

import cate.common.table.activity.weekendgift.row.WeekendGiftRow;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.common.util.TimeTool;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.weekendgift.msg.WeekendGiftInfoResp;
import cate.game.res.MixRes;
import easy.java.dev.note.NoteField;

public class WeekendGiftActivity extends RoleActivitySingle {
    @NoteField("今日奖励是否领取")
    public boolean took;


    @Override
    public void onDaySpan(boolean silence) {
        super.onDaySpan(silence);
        took = false;
        noticeUpdate();
    }

    @Override
    public void initSend() {
        super.initSend();
        noticeUpdate();
    }

    @Override
    public void onRoleEnd() {
        super.onRoleEnd();
        took = false;
    }

    public GameResult<WeekendGiftActivity> takeReward() {
        GameResult<WeekendGiftActivity> r = new GameResult<>();
        r.data = this;
        if (took) {
            return r.fail("奖励已领取");
        }
        int day = TimeTool.dayOfWeekCn(System.currentTimeMillis());
        WeekendGiftRow row = findRowByConfigAndDay(day);
        if (row == null) {
            return r.fail("奖励不存在");
        }
        this.took = true;
        MixRes res = new MixRes(row.reward);
        res.addAndDialog(role, true, GDOperation.WEEKEND_GIFT, row.id);
        return r;

    }

    private WeekendGiftRow findRowByConfigAndDay(int day) {
        return role.getGame().table.weekendGift.weekendGift.getRowByConfigIdAndDay(configTid(), day);
    }

    public void noticeUpdate() {
        role.sendNow(new WeekendGiftInfoResp(this));
    }
}
